package chat.network.http;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import inter.Delegate;

/**
 * Created by 凯 on 2015/10/24.
 */
public class GetRoomMessage {

    Delegate delegate;

    String user_ID;
    String room_ID;

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public void setUser_ID(String user_ID) {
        this.user_ID = user_ID;
    }

    public void setRoom_ID(String room_ID) {
        this.room_ID = room_ID;
    }

    // 获取群聊信息
    public void get() {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        final String url = "http://"+IP+"/Chat/GetRoomMessages";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (delegate != null)
                            delegate.onResponse(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if (delegate != null)
                            delegate.onErrorResponse(error);
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                map.put("user_ID",user_ID);
                map.put("room_ID",room_ID);
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }


    public static class RoomMsgs {
        private String user_ID;
        private String user_RealName;
        private String room_ID;
        private String room_Name;
        private String rm_Content;
        private String rm_Time;
        private String rm_Type;

        public String getUser_ID() {
            return user_ID;
        }
        public void setUser_ID(String user_ID) {
            this.user_ID = user_ID;
        }
        public String getUser_RealName() {
            return user_RealName;
        }
        public void setUser_RealName(String user_RealName) {
            this.user_RealName = user_RealName;
        }
        public String getRoom_ID() {
            return room_ID;
        }
        public void setRoom_ID(String room_ID) {
            this.room_ID = room_ID;
        }
        public String getRoom_Name() {
            return room_Name;
        }
        public void setRoom_Name(String room_Name) {
            this.room_Name = room_Name;
        }
        public String getRm_Content() {
            return rm_Content;
        }
        public void setRm_Content(String rm_Content) {
            this.rm_Content = rm_Content;
        }
        public String getRm_Time() {
            return rm_Time;
        }
        public void setRm_Time(String rm_Time) {
            this.rm_Time = rm_Time;
        }
        public String getRm_Type() {
            return rm_Type;
        }
        public void setRm_Type(String rm_Type) {
            this.rm_Type = rm_Type;
        }
    }

    public static class ResponseRoomMessages {
        private String status;
        private int unRead_Num;
        private List<RoomMsgs> rm_List;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public int getUnRead_Num() {
            return unRead_Num;
        }

        public void setUnRead_Num(int unRead_Num) {
            this.unRead_Num = unRead_Num;
        }

        public List<RoomMsgs> getRm_List() {
            return rm_List;
        }

        public void setRm_List(List<RoomMsgs> rm_List) {
            this.rm_List = rm_List;
        }
    }

}
